<template>
  <BasicModal v-bind="$attrs" @register="registerModal" :title="getTitle" @ok="handleSubmit">
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts" setup name="DictDataModal">
// defineOptions({name:"DictTypeModal"})
import { computed, ref, unref } from "vue";
import { BasicModal, useModalInner } from "@/components/Modal";
import { BasicForm, useForm } from "@/components/Form";
import { dictDataForm } from "./dict.data";
import {
  AddDictDataItem,
  UpdateDictDataItem
} from "@/api/system/model/DictModel";
import { AddDictData, UpdateDictData } from "@/api/system/dict";
import { notification } from "ant-design-vue";
import { useI18n } from '@/hooks/web/useI18n';
import { useDictStore } from "@/store/modules/dict";
const { t }  = useI18n();
//定义emit
const emit = defineEmits(["success", "register"]);
const isUpdate = ref(true); //是否更新
const dictType = ref('') //字典类型
let update: any; //更新数据
const getTitle = computed(() => (!unref(isUpdate) ? '新增字典' : '编辑字典'));
const [registerForm, { setFieldsValue, updateSchema, resetFields, validate }] = useForm({
  labelWidth: 100,
  schemas: dictDataForm,
  showActionButtonGroup: false,
  actionColOptions: {
    span: 23,
  },
});
const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
  await resetFields();
  setModalProps({ confirmLoading: false });
  isUpdate.value = !!data?.isUpdate;
  dictType.value = data.dictType
  update = data.record
  if (unref(isUpdate)) {
    await setFieldsValue({
      ...data.record,
    });
  }
  await setFieldsValue({
    dictType: dictType.value
  })
});
async function handleSubmit() {
  const values = await validate();
  try {
    setModalProps({ confirmLoading: true });
    if(unref(isUpdate)) {
      //更新
      let updateData:UpdateDictDataItem = {
        dictCode: update.dictCode,
        dictSort: values.dictSort,
        dictLabel: values.dictLabel,
        dictValue: values.dictValue,
        dictType: dictType.value,
        cssClass: values.cssClass,
        listClass: values.listClass,
        status: values.status,
        remark: values.remark
      }
      const res =  await UpdateDictData(updateData);
      if(res.code == 200) {
        notification.success({
          message: t('sys.update.success'),
          description: t('sys.update.success'),
          duration: 3,
        });
        useDictStore().delDict(dictType.value);
        closeModal();
        emit('success');
      }else{
        notification.error({
          message: t('sys.update.error'),
          description: res.msg,
          duration: 3,
        });
      }
    }else{
      //新增
      let addData:AddDictDataItem = {
        dictSort: values.dictSort,
        dictLabel: values.dictLabel,
        dictValue: values.dictValue,
        dictType: dictType.value,
        cssClass: values.cssClass,
        listClass: values.listClass,
        status: values.status,
        remark: values.remark
      }
      const res =  await AddDictData(addData)
      if(res.code == 200){
        notification.success({
          message: t('sys.add.addsuccess'),
          description: t('sys.add.addsuccess'),
          duration: 3,
        });
        useDictStore().delDict(dictType.value);
        closeModal();
        emit('success');
      }else{
        notification.error({
          message: t('sys.add.addfail'),
          description: res.msg,
          duration: 3,
        });
      }
      setModalProps({ confirmLoading: false });
    }
  } catch (error) {
    console.log(error)
  } finally {
    setModalProps({ confirmLoading: false });
  }
}
</script>
<style lang="scss" scoped>

</style>
